home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / Libraries / VideoToolbox 95.04.18 / Demos / Filter.c < prev    next >
C/C++ Source or Header  |  1995-04-12  |  7KB  |  215 lines

  1. /* Filter.c
  2. © 1989-1995 Denis G. Pelli 
  3. Display one letter and low-pass filter it.
  4. Also display a gray wedge, to confirm that lookup table has been correctly loaded.
  5.  
  6. HISTORY:
  7. 3/31/89 dgp Wrote it, based on simple program by Preeti Verghese & Mike Schechter,
  8.             which just displayed a character.
  9. 4/25/89 dgp added code to linearize the clut. Locked the window's pixmap.
  10. 10/9/89 dgp    Updated it to use SetLuminances, etc.
  11. 7/10/90 dgp Changed the declaration of window to be a CWindowPtr.
  12.             Added “done” message.
  13. 10/11/90 dgp Added fpu test.
  14. 10/12/90 dgp Changed LuminanceRecord.h to LuminanceRecord1.h
  15. 2/27/91    dgp     Tidied up.
  16. 8/24/91    dgp    Made compatible with THINK C 5.0.
  17. 2/15/92    dgp    Incorporated Larry Cormack's suggestion of shrinking the console
  18.             so as not to obscure the main window.
  19. 3/10/92    dgp    include mc68881.h
  20. 7/20/92    dgp    added printf of done message
  21. 8/27/92    dgp    replace SysEnvirons() by Gestalt()
  22. 1/4/93    dgp    Don't call SetLuminances if device is of fixedType.
  23. 2/7/93    dgp    Updated to use SetPixelsQuickly. Tidied up the grayscale wedge.
  24.             Recompiled with the new ConvolveX.c, which has better rounding.
  25. 2/8/93    dgp    Recompiled with the new ConvolveX.c, overflow bug now fixed.
  26. 7/7/93    dgp    Made rect a tad bigger to allow for slant of italic.
  27. 6/23/94    dgp    Use Choose().
  28. 4/11/95 dgp Save and restore the display depth and color mode.
  29. */
  30. #include "VideoToolbox.h"
  31. #include "Luminance.h"
  32. #include <math.h>
  33. #include <Fonts.h>
  34. #include <assert.h>
  35. #if UNIVERSAL_HEADERS
  36.     #include <LowMem.h>
  37. #else
  38.     #define LMGetMBarHeight() (* (short *) 0x0BAA)
  39.     #define LMSetMBarHeight(MBarHeightValue) ((* (short *) 0x0BAA) = (MBarHeightValue))
  40. #endif
  41. #if THINK_C
  42.     #include <console.h>
  43. #endif
  44. #if __MWERKS__
  45.     #include <SIOUX.h>
  46. #endif
  47.  
  48. #define SIZE 127        /* point size of text to be filtered */
  49. #define DIM 64            /* size of point spread function, sigma=DIM/4 */
  50.  
  51. void Filter(void);
  52.  
  53. void main(void)
  54. {
  55.     StackGrow(1000+sizeof(luminanceRecord)+512*sizeof(long));
  56.     Require(gestalt8BitQD);
  57.     Filter();
  58. }
  59. void Filter(void)
  60. {
  61.     EventRecord event;
  62.     register short i;
  63.     int j,clutSize,dim=DIM,error;
  64.     short textSize=SIZE,FontNum;
  65.     unsigned char *s;
  66.     char string[64];
  67.     Rect r,dstRect,wedge;
  68.     CWindowPtr window,oldPort;
  69.     GDHandle device,oldDevice;
  70.     double f[DIM],contrast,a,c,sigma;
  71.     luminanceRecord LR,*LP;
  72.     unsigned long row[512];
  73.     Boolean attenuator,oldIsColor;
  74.     RGBColor blackRGB={0,0,0},whiteRGB={0xffff,0xffff,0xffff};
  75.  
  76.     assert(StackSpace()>5000);
  77.     /* Gaussian point spread function, normalized to unit integral. */
  78.     sigma=dim/4.0;
  79.     c=0.0;
  80.     for(i=0;i<dim;i++) {
  81.         a=(i-(dim-1)/2.0)/sigma;
  82.         f[i]=exp(-a*a);
  83.         c+=f[i];
  84.     }
  85.     for(i=0;i<dim;i++) f[i] /= c;
  86.  
  87.     /* INITIALIZE QuickDraw */
  88.     #if (THINK_C || THINK_CPLUS)
  89.         console_options.nrows = 5;
  90.         console_options.left=32;
  91.     #elif __MWERKS__
  92.         SIOUXSettings.toppixel=LMGetMBarHeight()+19;    // allow for menu bar and title bar
  93.         SIOUXSettings.leftpixel=1;
  94.         SIOUXSettings.rows=5;
  95.         SIOUXSettings.autocloseonquit=0;
  96.         SIOUXSettings.showstatusline=0;
  97.         SIOUXSettings.asktosaveonclose=0;
  98.         printf("\n");
  99.     #elif
  100.         InitGraf(&qd.thePort);
  101.         InitFonts();
  102.         InitWindows();
  103.         InitCursor();
  104.     #endif
  105.     printf("\n");
  106.     GetGWorld(&oldPort,&oldDevice);
  107.  
  108.     printf("Welcome to Filter.\n");
  109.     /* Find device corresponding to the experimental screen. */
  110.     for(i=8;i>=0;i--){
  111.         device = GetScreenDevice(i);
  112.         if(device!=NULL) break;
  113.     }
  114.     do{
  115.         if(GetScreenDevice(1)!=NULL)i=ChooseScreen(i,"Which screen?");
  116.         else i=0;
  117.         device=GetScreenDevice(i);
  118.     }while(device==NULL);
  119.  
  120.     oldIsColor=TestDeviceAttribute(device,gdDevType);
  121.     clutSize=GDClutSize(device);
  122.     printf("%d colors.\n",clutSize);
  123.  
  124.     /* Use results of last screen calibration to do gamma correction */
  125.     #include "LuminanceRecord1.h"
  126.     LP=&LR;
  127.     attenuator=Choose(0,"Have you installed an ISR Video Attenuator on that monitor?\n"
  128.         ,noYes,2);
  129.     window = GDOpenWindow(device);
  130.     if(!attenuator){
  131.         LP->r=0.0;
  132.         LP->g=1.0;
  133.         LP->b=0.0;
  134.     }
  135.     // if not already in color mode, switch to color mode
  136.     if(!oldIsColor)error=SetDepth(device,(**(**device).gdPMap).pixelSize,1<<gdDevType,1);
  137.     if((*device)->gdType!=fixedType) SetLuminances(device,&LR,0,clutSize-1,0.0,LP->LMax);
  138.  
  139.     contrast = 1.0;
  140.     if(0){
  141.         printf("Contrast? (%f) ",contrast);
  142.         gets(string);
  143.         sscanf((char *)string,"%lf",&contrast);
  144.         printf("=%f\n",contrast);
  145.     }
  146.  
  147.     /* Display text */
  148.     SetPort((WindowPtr)window);
  149.     BringToFront((WindowPtr)window);
  150.     PmForeColor(0);                    /* black in our current clut */
  151.     PmBackColor(clutSize-1);        /* white in our current clut */
  152.     EraseRect(&window->portRect);    /* Fill with background color */
  153.     GetFNum((StringPtr)"\pHelvetica",&FontNum);
  154.     TextFont(FontNum);
  155.     TextFace(bold+italic);
  156.     TextSize(textSize);
  157.     j=(unsigned int)((1.0 - contrast)*(clutSize-1));
  158.     PmForeColor(j);
  159.     s = (unsigned char *)"\pHi";
  160.     SetRect(&r,0,0,StringWidth(s),textSize);
  161.     r.right+=0.3*textSize;    // Allow for slant of italic.
  162.     CenterRectInRect(&r,&window->portRect);
  163.     MoveTo(r.left,r.bottom);
  164.     DrawString(s);
  165.  
  166.     #if 1
  167.         /* Write a gray wedge, just for debugging, so we can examine clut */
  168.         SetRect(&wedge,0,0,20,256);
  169.         CenterRectInRect(&wedge,&window->portRect);
  170.         OffsetRect(&wedge,2-wedge.left,0);
  171.         for (j=0;j<256;j++){
  172.             row[0]=(long)clutSize*j/256;
  173.             for(i=0;i<20;i++)row[i]=row[0];
  174.             SetPixelsQuickly(wedge.left,j+wedge.top,row,20);
  175.         }
  176.         InsetRect(&wedge,-1,-1);
  177.         FrameRect(&wedge);
  178.     #endif
  179.  
  180.     /* Filter horizontally */
  181.     SetGDevice(device);                /* Use color table of that screen */
  182.     dstRect=r;                        /* dstRect just contains the string ... */
  183.     InsetRect(&dstRect,-dim/2,0);    /* extend dstRect to allow for the blur */
  184.     HLockHi((Handle)window->portPixMap);
  185.     ConvolveX(f,dim,(BitMap *) *window->portPixMap,(BitMap *) *window->portPixMap,
  186.         &window->portRect,&dstRect);
  187.  
  188.     /* Filter vertically */
  189.     InsetRect(&dstRect,0,-dim/2);    /* Extend dstRect to allow for the blur */
  190.     ConvolveY(f,dim,(BitMap *) *window->portPixMap,(BitMap *) *window->portPixMap,
  191.         &window->portRect,&dstRect);
  192.     SetGDevice(oldDevice);            /* Restore original device */
  193.  
  194.     GetFNum((StringPtr)"\pChicago",&FontNum);
  195.     TextFont(FontNum);
  196.     TextSize(12);
  197.     TextFace(0);
  198.     PmForeColor(0);                /* black in our current clut */
  199.     MoveTo(10,window->portRect.bottom-10);
  200.     DrawString((StringPtr)"\pDone. Click mouse or hit any key to exit.");
  201.     printf("Done. Click mouse or hit any key to exit.\n");
  202.     FlushEvents(everyEvent,0);
  203.     while(!GetNextEvent(keyDownMask+mDownMask,&event)) ;
  204.     SetGWorld(oldPort,oldDevice);
  205.     GDDisposeWindow(window);
  206.     // restore
  207.     error=SetDepth(device,(**(**device).gdPMap).pixelSize,1<<gdDevType,oldIsColor);
  208.     #if (THINK_C || THINK_CPLUS)
  209.         abort();
  210.     #elif __MWERKS__
  211.         SIOUXSettings.autocloseonquit=1;
  212.     #endif
  213. }
  214.  
  215.